package demo.jlistincheckbox;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 5/13/11
 * Time: 11:38 PM
 * To change this template use File | Settings | File Templates.
 */
public class DemoJList {

    public DemoJList() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JList list = new JList(new UserObject[]{
                new UserObject(true, "me", 37)
                , new UserObject(true, "mfdfe", 35)
                , new UserObject(false, "medf", 43)
                , new UserObject(false, "me", 83)
        });
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setCellRenderer(new CheckListRenderer());
        list.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JList list = (JList) e.getSource();
                int index = list.locationToIndex(e.getPoint());
                UserObject item = (UserObject) list.getModel().getElementAt(index);
//                System.out.println("value: " + item.getNameRu() + " " + item.getAge() + " " + item.isSelected());
                item.setSelected(!item.isSelected());
                list.repaint(list.getCellBounds(index, index));

            }
        });
        frame.getContentPane().add(new JScrollPane(list));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new DemoJList();
    }
}
